/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.app.demos.base;

//***rest****
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.json.JSONArray;
import org.json.JSONException;
//import org.json.JSONObject;

import android.util.Log;

/**
 * 
 * @author Administrator
 */
public class Http {
	public String sendGet(String url) {
		HttpGet httpGet = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		StringBuilder builder = new StringBuilder();
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int code = statusLine.getStatusCode();
			if (code == 200) {
				HttpEntity entity = response.getEntity();
				InputStream html = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(html));
				String line;
				if ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.e(Http.class.toString(), "Failed to Get");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return builder.toString();
	}

	public String sendPost(String url, HashMap<String, String> postval) {
		HttpPost httpPost = new HttpPost(url);
		HttpClient client = new DefaultHttpClient();
		StringBuilder builder = new StringBuilder();
		Map<String, String> parmas = postval;
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		if (parmas != null) {
			Set<String> keys = parmas.keySet();
			for (Iterator<String> i = keys.iterator(); i.hasNext();) {
				String key = (String) i.next();
				pairs.add(new BasicNameValuePair(key, parmas.get(key)));
			}
		}
		try {
			UrlEncodedFormEntity entity_p = new UrlEncodedFormEntity(pairs,
					"utf-8");
			httpPost.setEntity(entity_p);

			HttpResponse response = client.execute(httpPost);	
			HttpEntity entity = response.getEntity();
			InputStream html = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					html));
			String line;
			if ((line = reader.readLine()) != null) {
				builder.append(line);
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.e(Http.class.toString(), "Failed to post");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return builder.toString();
	}

	public JSONArray strtoarray(String str) {
		try {
			JSONArray jsonarray = new JSONArray(str);
			return jsonarray;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public void StrtoJson() {

	}
}
